<?php 

/**
 * Joomla! 1.5 component arts_curriculum
 * @package com_arts_curriculum
 * @author    Chris Borkowski {@link http://www.guggenheim.org}
 * @author    Created on 15 July-2010
 * @author    Modified on 25-August-2010

 **/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


/**
* @package arts_curriculum
* things to help out on the AC front end
* @ acess public
*/

	class Arts_curriculumFrontHelper {
	
		/* Method
		 * makes thumnails on the fly for picture with caption in overview tab
		 * @ access public
		 */
		
		function checkderivative1($file_name) {
	  	
	  	global $mainframe ;
	  	
	  	$check_file = JPATH_ROOT . DS . "images". DS ."content" . DS ."arts_curriculum". DS . "thumbs2". DS .$file_name ;
	  	
		if (file_exists($check_file)) {
		return true;
		} else {

		//contants for size and quality can be 120, 205 or what ever - this has changed alot
		$h_targ_w = 205;
		$jpeg_quality = 100;
		
		// where the derivatives live 
		$uploads_path = JPATH_ROOT . DS . "images". DS ."content" . DS ."arts_curriculum". DS . "masters" ;
		// where the files will be placed
		$thumb_path = JPATH_ROOT . DS . "images". DS ."content" . DS ."arts_curriculum". DS . "thumbs2" ;
		
		//get some info from image
		$image_stats = GetImageSize($uploads_path.'/'.$file_name);
		$imagewidth = $image_stats[0];
		$imageheight = $image_stats[1];

	
		//do math to determine height based on fixed width
		$ratio = ($imagewidth / $h_targ_w);
		$new_h = round($imageheight / $ratio);

		$src_img = imagecreatefromjpeg($uploads_path."/".$file_name);
		$dst_img = imagecreatetruecolor($h_targ_w,$new_h);
		
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$h_targ_w,$new_h,imagesx($src_img),imagesy($src_img));
		imagejpeg($dst_img, $thumb_path."/".$file_name, $jpeg_quality);						
		imagedestroy($dst_img);
		
		}//end if
	
  
  	}//end function	
	
	
		
		/* Method
		 * makes thumnails on the fly for picture in exhibitions list
		 * @ access public
		 */
		
		function checkderivative2($file_name) {
	  	
	  	global $mainframe ;
	  	
	  	$check_file = JPATH_ROOT . DS . "images". DS ."content" . DS ."arts_curriculum". DS . "derivative2". DS .$file_name ;
	  	
		if (file_exists($check_file)) {
		return true;
		} else {

		//contants for size and quality can be 120, 205 or what ever - this has changed alot
		$h_targ_w = 120;
		$jpeg_quality = 100;
		
		// where the derivatives live 
		$uploads_path = JPATH_ROOT . DS . "images". DS ."content" . DS ."arts_curriculum". DS . "derivative1" ;
		// where the files will be placed
		$thumb_path = JPATH_ROOT . DS . "images". DS ."content" . DS ."arts_curriculum". DS . "derivative2" ;
		
		//get some info from image
		$image_stats = GetImageSize($uploads_path.'/'.$file_name);
		$imagewidth = $image_stats[0];
		$imageheight = $image_stats[1];

	
		//do math to determine height based on fixed width
		$ratio = ($imagewidth / $h_targ_w);
		$new_h = round($imageheight / $ratio);

		$src_img = imagecreatefromjpeg($uploads_path."/".$file_name);
		$dst_img = imagecreatetruecolor($h_targ_w,$new_h);
		
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$h_targ_w,$new_h,imagesx($src_img),imagesy($src_img));
		imagejpeg($dst_img, $thumb_path."/".$file_name, $jpeg_quality);						
		imagedestroy($dst_img);
		
		}//end if
	
  
  	}//end function
	
		
		/* Method
		 * takes item array flattens it into single dimenssion array 
		 * and destroys duplicate keys that have same values
		 * used in artist sort list
		 * @access public
		 * @return array
		 */
		
		function cleansort(& $sorted) {
	  	
		global $mainframe ;
		
		$newitems = array();
    	foreach ($sorted as $item){
    	
    	$newitems[] = $item->artist_name_link;
    	$newitems[] = $item->artist_less_or_ex_link;
    	$newitems[] = $item->link;
    	}
    	
    	$results = array_unique($newitems);
	   	
	   	foreach($results as $key => $value) {
   		echo $value;
		}		
  
	}//end funtion   

	
		/* Method
		 * takes item array flattens it into single dimenssion array 
		 * and destroys duplicate keys that have same values
		 * @access public
		 * @return array
		 */
		
		function cleansort2( $sorted) {
	  	
		global $mainframe ;
		
		
		$newitems = array();
     	
		foreach ($sorted as $item){
    	
    	$newitems[] = $item->catlink; 
    	$newitems[] = $item->link;
    	//$newitems[] = $item->subject; 
    	}
  	
    	//print_r ($newitems);
    	
    	$results = array_unique($newitems);
    	
	   	foreach($results as $key => $value) {
	   	
   		echo $value;
		}	
  
	}//end funtion  
	
	   
	    /* Method
		 * we need to make a subject link - this is tricky - first break the string items into array then process each array item
		 * explode the string by the "," delimeter and cast into an subjects_array 
		 * then process all but the last item in the array.
		 * the last item in the array is processed outside the for loop becasue we don't add a comma to it
		 *
		 * @access public
		 * returns string
		 */
	
		function subjectlinks($subjects){
		
		global $mainframe ;
		
		$subjects_array = explode(",", $subjects);
		$scount = count($subjects_array);
		$i=0;
		$html= '';
		while ( $i < $scount-1) {
			$sublink = JRoute::_( 'index.php?option=com_arts_curriculum&view=sort&search='.$subjects_array[$i]);
			$html .= '<a href="'. $sublink .'" class="ac_sort_sub_link">'.$subjects_array[$i].',</a>&nbsp;';
			$i++;
			} 
		
		//make the last link without a comma
		$sublink2 = JRoute::_( 'index.php?option=com_arts_curriculum&view=sort&search='.$subjects_array[$scount-1]);
		$html .= '<a href="'. $sublink2 .'" class="ac_sort_sub_link">'.$subjects_array[$scount-1].'</a>';
				
		return $html;
		
	}// end frunction
	
	
		/*Method
		 *we need to make a subject/meduim link - this is tricky - we need to break the string items into array then process each array item
		 * split the string by the "," delimeter and cast into an array via explode
		 *
		 * @access public
		 * returns string
		*/
	
	
		function subjectlinks2($subjects){
		
		global $mainframe ;
		
		$sublink ='';
		$subjects_array = explode(",", $subjects);
		foreach ($subjects_array as $subject){
		
		// if linkable is needed use below
			$sublinks = JRoute::_( 'index.php?option=com_arts_curriculum&view=sort&search='.$subject);
			$sublink .= '<div class="ac_cat_sub_link">'.$subject.'</div>';
			}
		
		return $sublink;
	
	}// end frunction
	
			
		/* Method
		 * create nicely formatted HTML for thubmail images and lightbox pop ups.
		 * @access public
		 * returns string
		 */
	
	
		function lightboxthumbs($image, $title, $artist, $cap, $n ){
		
		global $mainframe ;
		
		$thumbs_path =  'images'. DS .'content' . DS .'arts_curriculum'. DS . 'thumbs'. DS;
		$masters_path =  'images'. DS .'content' . DS .'arts_curriculum'. DS . 'masters'. DS;
		$dloads_path =  'images'. DS .'content' . DS .'arts_curriculum'. DS . 'dloads'. DS;
		
		$image_stats = GetImageSize($thumbs_path.$image);
		$imageheight = $image_stats[1];
		$shim_height = (195 - $imageheight)/2;

		$html ='<!-- image'.$n. ' //output generated dynamicaly -->';
		$html.='<div id="ac_image'.$n.'_wrap">';
		$html.='<a id="ac_enlarge-collection" href="'.$masters_path.$image.'#TB_inline?height=657&amp;width=525&amp;inlineId=full-image'.$n.'" class="thickbox enlarge show-slideshow">Enlarge</a>';
		$html.='<div class="ac_collection-space">';
		$html.='<div id="ac_image'.$n.'">';
		$html.='	<div id="ac_image_shim" style="height:'.$shim_height.'px; display:block;">&nbsp;</div>';
		$html.='		<a href="'.$masters_path.$image.'#TB_inline?height=657&amp;width=525&amp;inlineId=full-image'.$n.'" class="thickbox show-slideshow">';
		$html.='		<img src="'.$thumbs_path.$image.'" alt="'.$title.'"  /></a></div>';
		$html.='		</div>';
		$html.='</div><!--end visible-->';
		$html.='<!--this all hides-->';
		$html.='<div id="full-image'.$n.'" class="full-image-hidden">';
		$html.='<div id="slideshow-loader"><img src="/templates/guggenheim/images/ajax-loader.gif" /></div>';
		$html.='<div id="piece-container">';
		$html.='<h2 id="image-artist-name">'.$artist .'</h2>';
		$html.='	<img id="full-image-img" src="'.$masters_path.$image.'" />';
		$html.='	<p id="image-caption">'. $cap.'</p>';
		$html.='	<div id="ac_dload"><a href="'.$dloads_path.$image.'" />Download image</a></div>';
		$html.='</div>';
		$html.='<script type="text/javascript">dontClearCollectionsLightbox=true;</script>';
		$html.='</div>';
		
		echo $html;
	}	//end function	
	
		
		/* Method
		 * mimics a Joomla style pugin. looks for {ac-thumbcaps} and 
		 * creates nicely formatted HTML for thubmail images with captions.
		 * @access public
		 * returns string
		 */
	
		function acThumbcaps($image, $title, $caption, $textbody){
		
		global $mainframe ;
	
		$thumb_file = 'images'. DS .'content' . DS .'arts_curriculum'. DS . 'thumbs2'. DS .$image ;
	
		$replace = '<div class="row-with-pic">';
		$replace.=		'<div class="row-picture" style="width:215px; margin-bottom:0px;">'; 
	 	$replace.=	 	'<img alt="'.$title.'" src="'.$thumb_file.'"/>';
 		$replace.=	  	'<p class="row-caption"style="width:205px;">'.$caption.'</p>';
		$replace.=	   	'</div>';
		$replace.=			'<div class="row-text"style="margin:0px;">$1</div>';
		$replace.=	'</div>';
		
		echo preg_replace('/\{ac-thumbcap}(.*)\{\/ac-thumbcap\}/sU', $replace, $textbody);
			
		
	}//end funtion
	
	  
}//the end
